package yambal.net.google.Spreadsheet
{
	import com.adobe.utils.DateUtil;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	import mx.collections.ArrayCollection;
	
	import yambal.net.google.Authentication;
	
	public class Spreadsheets
	{
		public var Auth:Authentication;
		private var Sheets:ArrayCollection;
		
		public function Spreadsheets()
		{
			this.busy_child = new Array();
		}
		
		// ----------------------------------------------------------
		/**
		 * busyコントロール
		 **/
		private var _isBusy:Boolean = false;
		private function isBusy(f:Boolean):void{
			if(this._isBusy != f){
				this._isBusy = f;
				trace("Busy:" + this._isBusy);
			}
		}
		
		// 自分自身のBusy
		private var _iAmBusy:Boolean = false;
		private function iAmBusy(f:Boolean):void{
			if(this._iAmBusy != f){
				this._iAmBusy = f;
			}
			
			if(!this.ChildIsBusy && !this._iAmBusy){
				this.isBusy(false);
			}else{
				this.isBusy(true);
			}
		}
		
		// 子のBusy(子から呼び出し)
		private var busy_child:Array;
		private var ChildIsBusy:Boolean = false;
		public function childIsBusy(f:Boolean, id:String):void{
			var test:int = this.busy_child.indexOf(id);
			if(test==-1 && f){
				this.busy_child.push(id);
				
			}else if(test!=-1 && !f){
				var h:Array = new Array();
				if(test != 0){
					h = this.busy_child.slice(0, test-1);
				}
				var b:Array = this.busy_child.slice(test+1);
				this.busy_child = h.concat(b);
				
			}
			this.ChildIsBusy = this.busy_child.length != 0
			
			if(!this.ChildIsBusy && !this._iAmBusy){
				this.isBusy(false);
			}else{
				this.isBusy(true);
			}
		}

		// ----------------------------------------------------------
		/**
		 * Authenticate
		 * 	もし、認証が必要な操作を行う場合は
		 *	setAuthenticateを行います。
		 **/
		public function setAuthenticate(email:String, password:String, isHosted:Boolean = false):void{
			this.iAmBusy(true);
			
			var auth:Authentication = new Authentication();

			auth.addEventListener(Event.COMPLETE, this.onAuthenticate); 
			auth.authenticate(email, password, 'wise', isHosted);
		}
		
		private function onAuthenticate(e:Event):void{
			var auth:Authentication = e.currentTarget as Authentication;
			auth.removeEventListener(Event.COMPLETE, this.onAuthenticate);
			
			this.Auth = auth;
			
			this.iAmBusy(false);
			
			this.setSheetList();
		}
		
		public function setSheetList():void{
			this.iAmBusy(true);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.onResponseGetSheetList);
			
			var req:URLRequest = new URLRequest();
			req.url = "http://spreadsheets.google.com/feeds/spreadsheets/private/full";
			req.method = URLRequestMethod.GET;
			
			req.requestHeaders.push(new URLRequestHeader('Authorization', 'GoogleLogin auth=' + this.Auth.auth));
			req.requestHeaders.push(new URLRequestHeader('Content-Type','application/atom+xml'));
			req.requestHeaders.push(new URLRequestHeader('GData-Version', '2'));
			loader.load(req);
		}
		
		private function onResponseGetSheetList(e:Event):void{
			var loader:URLLoader = e.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.onResponseGetSheetList);
			
			var res:XML = new XML(loader.data);
			
			var atom:Namespace = res.namespace();
			var openSearch:Namespace = res.namespace('openSearch');
			var gd:Namespace = res.namespace('gd');
			
			var sheets:Array = new Array();
			for each (var entry:XML in res.atom::entry) {
				var sheet:Sheet = this.parseToSheet(entry);
				sheets.push(sheet);
			}
			this.Sheets= new ArrayCollection(sheets);
			
			this.iAmBusy(false);
			//this.dispatchEvent(new PicasaEvent(PicasaEvent.ALBUM_LIST, albums));
		}
		
		// -----------------------------------------------------------------------------------------
		/**
		 * EntryをSheetにパースする
		 **/
		private function parseToSheet(entry:XML):Sheet{
			var atom:Namespace = entry.namespace();
			
			var sheet:Sheet = new Sheet();
			sheet.spreadsheet = this;
				
			// parse entry
			sheet.id = entry.atom::id;
			sheet.updated = DateUtil.parseW3CDTF(entry.atom::updated);
			sheet.title = entry.atom::title;
			sheet.content_url = entry.atom::content.@src;
			
			return sheet;
		}
		
	}
}